package com.wangbo.ocr;

import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class TestBaiduOCR {

    public static final String AppID = "24737310";
    public static final String APIKey = "XcU6560LcCgj1hER8bTI23ty";
    public static final String SecretKey = "yio7xN8pLQ3dPXAMj8s563YoBGB7isuK";
    public static final String IMG_BASE = "D:\\case\\%s";


    public static void main(String[] args) {
        AipOcr client = new AipOcr(AppID, APIKey, SecretKey);

        test02(client,"186xxxx9013","D:\\temp\\微信图片_20211021155426.jpg");
    }

    /**
     * 苏康码测试
     */
    public static void test01(AipOcr client, String name, String path) {
        boolean condition1 = false;
        String pattern1 = "苏康码";

        boolean condition2 = false;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MM-dd");
        String pattern2 = dateTimeFormatter.format(LocalDate.now());

        boolean condition3 = false;
        String pattern3 = String.format("姓名:%s", name);

        JSONObject jsonObject = client.basicAccurateGeneral(path, null);
        for (Object obj : jsonObject.getJSONArray("words_result")) {
            String str = (String) ((JSONObject) obj).get("words");
            System.out.println(str);
            if (str.contains(pattern1)) {
                condition1 = true;
            }
            if (str.contains(pattern2)) {
                condition2 = true;
            }
            if (str.contains(pattern3)) {
                condition3 = true;
            }
        }

        if (!condition1) {
            System.out.printf("400, %s%n","请上传真实有效的苏康码");
        }
        if (!condition2) {
            System.out.printf("400, %s%n","苏康码日期不匹配");
        }
        if (!condition3) {
            System.out.printf("400, %s%n","苏康码姓名不匹配");
        }
    }

    /**
     * 行程码测试
     */
    public static void test02(AipOcr client, String phone, String path) {
        boolean condition1 = false;
        String pattern1 = "行程卡";

        boolean condition2 = false;
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
        String pattern2 = dateTimeFormatter.format(LocalDate.now());

        boolean condition3 = false;

        JSONObject jsonObject = client.basicAccurateGeneral(path, null);
        for (Object obj : jsonObject.getJSONArray("words_result")) {
            String str = (String) ((JSONObject) obj).get("words");
            System.out.println(str);
            if (str.contains(pattern1)) {
                condition1 = true;
            }
            if (str.contains(pattern2)) {
                condition2 = true;
            }
            if (str.contains(phone.substring(0, 3)) && str.contains(phone.substring(8, 11))) {
                condition3 = true;
            }
        }

        if (!condition1) {
            System.out.printf("400, %s%n","请上传真实有效的行程码");
        }
        if (!condition2) {
            System.out.printf("400, %s%n","行程码日期不匹配");
        }
        if (!condition3) {
            System.out.printf("400, %s%n","行程码姓名不匹配");
        }
    }
}
